﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace Susam
{
    class ListBoxManager
    {
        private ListBox _lb;
        private Label _pivot;
        delegate void ListBoxOperationCallback();
        public ListBoxManager(ListBox listBox, Label label)
        {
            _lb = listBox;
            _pivot = label;
        }

        public void PopulatePeopleList()
        {
            // This pattern is needed for thread safety.
            if (this._lb.InvokeRequired)
            {
                ListBoxOperationCallback d = new ListBoxOperationCallback(PopulatePeopleList);
                this._lb.Invoke(d);
            }
            else
            {
                //_lb.BackColor = System.Drawing.ColorTranslator.FromHtml("#E0ECF8");
                _lb.Tag = new BooksOfPerson();
                _pivot.Text = "People List";
                //_pivot.BackColor = Color.DarkBlue;
                _lb.Items.Clear();
                IEnumerable<susamDataSet.tblPersonRow> peopleList = null;
                List<String> roles = new List<String>();
                switch (Properties.Settings.Default.ContributorRoleSelection)
                {
                    case (int)RoleSelection.None: 
                        roles.Add("aut");
                        peopleList = DatabaseManager.Instance.GetPeopleList(roles);
                        break;
                    case (int)RoleSelection.All:
                        peopleList = DatabaseManager.Instance.GetPeopleList();
                        break;
                    case (int)RoleSelection.Selected:
                        roles.Add("aut");
                        if (Properties.Settings.Default.IncludeTranslatorRole) roles.Add("trl");
                        if (Properties.Settings.Default.IncludeEditorRole) roles.Add("edt");
                        if (Properties.Settings.Default.IncludeIllustratorRole) roles.Add("ill");
                        if (Properties.Settings.Default.IncludeRedactorRole) roles.Add("red");

                        peopleList = DatabaseManager.Instance.GetPeopleList(roles);

                        break;
                    default:
                        Debug.WriteLine("Invalid setting!");
                        break;
                }
                foreach (var row in peopleList)
                {
                    Pair p = new Pair(row.PersonID, row.PersonName);
                    _lb.Items.Add(p);
                }
            }
        }

        public void PopulateFileTypeList()
        {
            //_lb.BackColor = System.Drawing.ColorTranslator.FromHtml("#ECF8E0");
            _lb.Tag = new BooksOfFileType();
            _pivot.Text = "File Types";
            //_pivot.BackColor = Color.Green;
            _lb.Items.Clear();
            foreach (var row in DatabaseManager.Instance.GetFileTypeList())
            {
                Pair p = new Pair(row.FileTypeID, row.FileTypeName);
                _lb.Items.Add(p);
            }
        }

        // TODO: ListBox background color?
        public void PopulateLanguageList()
        {
            //_lb.BackColor = System.Drawing.ColorTranslator.FromHtml("#F7F8E0");
            _lb.Tag = new BooksOfLanguage();
            _pivot.Text = "Languages";
            //_pivot.BackColor = Color.DarkOrange;
            _lb.Items.Clear();
            foreach (var row in DatabaseManager.Instance.GetLanguageList())
            {
                Pair p = new Pair(row.LanguageID, row.LanguageName);
                _lb.Items.Add(p);
            }
        }

        public void ClearSelected()
        {
            _lb.ClearSelected();
        }
    }
}
